﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace devicesDll
{
    public delegate void QueryFinishedEventHandler(object sender, EventArgs e);

    class CustomThread
    {
        public event QueryFinishedEventHandler QueryFinished;

        #region Variables
        volatile Device currentDevice=null;
        volatile string currentQuery=null;
        volatile bool currentSwitchOnly=false;
        volatile bool isWorking = false;
        volatile bool shouldStop = false;
        #endregion

        #region Consturctor

        /// <summary>
        /// empty Constructor, nothing is done until new data is inserted
        /// </summary>
        public CustomThread()
        {
        }

        /// <summary>
        /// Consturctor
        /// </summary>
        /// <param name="currentDevice">First device to check</param>
        /// <param name="switchOnly"><para>if only switches should be questioned: true</para>
        /// <para>if false: only other devices are questioned</para></param>
        /// <param name="query">The query which should be run</param>
        public CustomThread(Device currentDevice, bool switchOnly, string query)
        {
            this.currentDevice = currentDevice;
            this.currentQuery = query;
            this.currentSwitchOnly = switchOnly;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Thread-Method only for queries
        /// </summary>
        public void QueryThread()
        {
            while (!shouldStop)
            {
                while (currentDevice == null) ; //wait until new Device and query is given

                this.isWorking = true;
                this.currentDevice.Query(currentSwitchOnly, currentQuery);
                this.ResetVariables();
                this.isWorking = false;

                if (QueryFinished != null)  //if Event is hooked up
                    QueryFinished(this, EventArgs.Empty); //send event
            }
        }

        /// <summary>
        /// Give New Informations for another query
        /// </summary>
        /// <param name="currentDevice">Device to check</param>
        /// <param name="switchOnly"><para>if only switches should be questioned: true</para>
        /// <para>if false: only other devices are questioned</para></param>
        /// <param name="query">The query which should be run</param>
        /// <returns>setting Variables is successful</returns>
        public bool SetNewQuery(Device currentDevice, bool switchOnly, string query)
        {
            if (isWorking)
                return false;

            this.currentQuery = query;
            this.currentSwitchOnly = switchOnly;
            this.currentDevice = currentDevice;
            return true;
        }

        public void StopThread()
        {
            shouldStop = true;
        }

        private void ResetVariables()
        {
            this.currentQuery = null;
            this.currentDevice = null;
            this.currentSwitchOnly = false;
        }
        #endregion

        #region Porperties
        public bool IsWorking { get { return isWorking; } }
        #endregion
    }
}
